<template>
  <el-button @click="preview">点击报表预览</el-button>
  <el-button plain @click="_PrintInfo=!_PrintInfo;console.log(_PrintInfo)">切换打印信息{{ _PrintInfo }}</el-button>
  <el-button @click="xkprint('pdf')">导出pdf</el-button>
  <el-button @click="xkprint('print')">打印</el-button>
  <el-button @click="excelExport">导出excel</el-button>
  <el-button @click="customPrint">自定义打印</el-button>
  <el-button @click="previewPrint">打印预览</el-button>
  <div style="height: 100%" id="container_indentorEcho"></div>
</template>
<script setup>


const reportDesignerId = "container_indentorMain"
import {usePreviewStore} from "@/stores/modules/xzPreviewStore";
import {onMounted, ref} from "vue";
import request from "@/utils/request.ts";
let _PrintInfo = ref(false);
import {
  union_onUpdateDataMaps,
  union_onUpdateDataSetList,
  filterDatasetsFrom_params
} from "@/utils/xzToolsUtil";
import {
  get_onupdatedatamaps as get_onupdatedatamaps_indentor_Echo
} from "@/views/reportDesigner/indentor/get_onUpdateDataMaps_Echo.js";
import {
  get_onupdatedatamaps as get_onupdatedatamaps_qoq
} from "@/views/reportDesigner/qoq/get_onUpdateDataMaps_qoq.js";
import {
  get_onupdatedatamaps as get_onupdatedatamaps_accu_lbl
} from "@/views/reportDesigner/accumulate_LayerByLayer/get_onUpdateDataMaps_accu_lbl.js";
import {
  get_onupdatedatamaps as get_onupdatedatamaps_accu_cross
} from "@/views/reportDesigner/accumulate_cross/get_onUpdateDataMaps_accu_cross.js";

const get_onupdatedatamaps = () => {
  let all_onupdatedatamaps = new Map();
  union_onUpdateDataMaps(get_onupdatedatamaps_indentor_Echo(), all_onupdatedatamaps);
  union_onUpdateDataMaps(get_onupdatedatamaps_qoq(), all_onupdatedatamaps);
  union_onUpdateDataMaps(get_onupdatedatamaps_accu_lbl(), all_onupdatedatamaps);
  union_onUpdateDataMaps(get_onupdatedatamaps_accu_cross(), all_onupdatedatamaps);
if(_PrintInfo)
  console.log("=============》all_onupdatedatamaps：", all_onupdatedatamaps);

  return all_onupdatedatamaps;
}


import {
  get_onUpdateDatasetlist as get_onUpdateDatasetlist_indentor
} from "@/views/reportDesigner/indentor/get_onUpdateDatasetList.js";
import {
  get_onUpdateDatasetlist as get_onUpdateDatasetList_qoq
} from "@/views/reportDesigner/qoq/get_onUpdateDatasetList_qoq.js";
import {
  get_onUpdateDatasetlist as get_onUpdateDatasetlist_accu_lbl
} from "@/views/reportDesigner/accumulate_LayerByLayer/get_onUpdateDatasetList_accu_lbl.js";
import {
  get_onUpdateDatasetlist as get_onUpdateDatasetlist_accu_cross
} from "@/views/reportDesigner/accumulate_cross/get_onUpdateDatasetList_accu_cross.js";

let results = [];
union_onUpdateDataSetList(get_onUpdateDatasetlist_indentor(), results);
union_onUpdateDataSetList(get_onUpdateDatasetList_qoq(), results);
union_onUpdateDataSetList(get_onUpdateDatasetlist_accu_lbl(), results);
union_onUpdateDataSetList(get_onUpdateDatasetlist_accu_cross(), results);


const usePreStore = usePreviewStore();
let spreadSheet;
//初始数据；假定报表设计器是根据这个这个初始数据的表结构设计的，回显可以用这个数据；否则。。。。
import {option} from "@/views/reportDesigner/indentor/option_indentor.js";
let reportPrintOptions=[
  {
    name:"主子表",
    //print:config,
    print:null,
  }
]
let reportPrint;
let reportPrintData;


onMounted(() => {
  spreadSheet = XZReport("#container_indentorEcho", option);
  reportPrint = XZReportPrint();
  reportPrint.setTemplate(reportPrintOptions);
  //spreadSheet = XZReport("#reportPreview", option, config, data);
})
const preview = () => {
  if (usePreStore.xzReportDesigners.size > 0 && usePreStore.xzReportDesigners.has(reportDesignerId)) {
    let {config, data} = usePreStore.xzReportDesigners.get(reportDesignerId)
    reportPrintData=data;
    reportPrintOptions[0].print=config;
    reportPrint.setTemplate(reportPrintOptions);

    if (data.length > 0) {//有json数据源
     if(_PrintInfo)
      console.log("报表设计器实例sheetDesign.getData()， {config, data}有json数据源：", data);
      spreadSheet.update(config, data);

    } else {//没有json数据源，只有外部数据源

      //spreadSheet = XZReport("#container_indentorEcho", option);
      spreadSheet.update(config);
    }
  }else {
    document.getElementById("container_indentorEcho").innerHTML = "";//不可以缺少，因为点击preview时候，config是有可能变了。
    spreadSheet = XZReport("#container_indentorEcho", option);

  }
}


const onUpdateData = (datasets, params) => {
  console.log("datasets:", datasets, "params:", params);
    return new Promise((resolve, reject) => {
      let __rtn;
      try {
        __rtn = filterDatasetsFrom_params(datasets, params, results, get_onupdatedatamaps);
      } catch (error) {
        console.log(error);
      }
      resolve(__rtn);
    })
}


option.onUpdateData = onUpdateData

function xkprint(printmode) {
  //提供了两种打印方法, 表格实例直接调用即可
//  print 方法
// option 可选值  {mode:"print"}  print:直接打印 pdf:转pdf打印
  option.mode = printmode
  spreadSheet.print({mode: "print"});

// implicitPrint 方法   参数同pivotData方法
// data 传入二维数组是批量数据打印
// option 可选值  {mode:"print"}  print:直接打印 pdf:转pdf打印
  // spreadSheet.implicitPrint(sheetConfig,data,option?)

}

function excelExport() {

// @param {*} options {fileName: 'download', // 下载文件名 exportHideCol: true, // 是否导出隐藏的行列 exportFormula: true, // 是否导出公式

  spreadSheet.exportExcel({fileName: 'excel1', exportHideCol: true, exportFormula: true})
}

function customPrint() {
  console.log("reportPrint.getTemplate---->", reportPrint.getTemplate);
  reportPrint.print(reportPrintData);


}

function previewPrint() {
  try{
    console.log(reportPrintData);
    reportPrint.previewPrint(reportPrintData);
  }catch (error){
    console.log(error);
  }

}
</script>

<style scoped>

</style>
